package whf.framework.web.filter;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import whf.framework.log.Log;
import whf.framework.log.LogFactory;
import whf.framework.log.util.LogUtil;
import whf.framework.util.StringUtils;
import whf.framework.util.ThreadContext;
import whf.framework.util.Utils;
import whf.framework.web.WebContextHolder;

/**
 * 1.设置当前UserContextHolder的用户
 * 2.完成异常配置捕捉
 * @author wanghaifeng
 * @create Aug 25, 2006 10:07:56 PM
 * 
 */
public class UserContextFilter implements Filter {
	private static Log log = LogFactory.getLog(UserContextFilter.class);
	
	private Map<String, String> configs = Utils.newHashMap();
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	 */
	public void init(FilterConfig config) throws ServletException {
		Enumeration names = config.getInitParameterNames();
		while(names != null && names.hasMoreElements()){
			String name = (String)names.nextElement();
			String value = config.getInitParameter(name);
			if(!StringUtils.isEmpty(name) && !StringUtils.isEmpty(value)){
				this.configs.put(name, value);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
	 *      javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = ((HttpServletRequest)request);
		String uri = req.getRequestURI();
		String scp = req.getContextPath()+"/services/";
		if(uri != null && !uri.startsWith(scp)){
			try {
				WebContextHolder webContext = new WebContextHolder(req);
				ThreadContext.setUserContext(webContext.getUserContext());	//设置当前用户上下文
			} catch (Exception e) {
				log.info(e);
			}
		}
		try{
			long start = System.currentTimeMillis();
			chain.doFilter(request, response);
			if(uri.endsWith(".do") || uri.endsWith(".jsp"))
				log.debug("Request \t" + uri + ":" + (System.currentTimeMillis() - start) + " !!!!\n");
		}catch(Exception e){
			request.setAttribute("exception", e);
			String page = this.configs.get(e.getClass().getName());
			if(page == null){
				page = this.configs.get(Exception.class.getName());
			}
			if(!StringUtils.isEmpty(page)){
				((HttpServletResponse)response).sendRedirect(((HttpServletRequest)request).getContextPath()+page);
			} else {
				LogUtil.logAccessError((HttpServletRequest)request, "error", ((HttpServletRequest)request).getRequestURI(), e, 0);
				throw new ServletException(e);
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.Filter#destroy()
	 */
	public void destroy() {
		this.configs.clear();
		this.configs = null;
	}

}
